﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Models
{
    public class BlogInfo
    {
        /// <summary>
        /// 博客ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 博客标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 博客内容
        /// </summary>
        public string BlogContent { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int AuthorID { get; set; }
        /// <summary>
        /// 分类ID
        /// </summary>
        public int CategoryID { get; set; }
        /// <summary>
        /// 是否精华
        /// </summary>
        public bool IsEssence { get; set; }
        /// <summary>
        /// 是否推荐
        /// </summary>
        public bool IsRecommand { get; set; }
        /// <summary>
        /// 链接地址
        /// </summary>
        public string LinkUrl { get; set; }
        /// <summary>
        /// 发表时间
        /// </summary>
        public DateTime PublishTime { get; set; }

        public BlogInfo() { }
        public BlogInfo(int id, string title, string blogContent, int authorID, int categoryID, bool isEssence, bool isRecommand, string linkUrl, DateTime publishTime)
        {
            this.ID = id;
            this.Title = title;
            this.BlogContent = blogContent;
            this.AuthorID = authorID;
            this.CategoryID = categoryID;
            this.IsEssence = isEssence;
            this.IsRecommand = isRecommand;
            this.LinkUrl = linkUrl;
            this.PublishTime = publishTime;
        }
        public BlogInfo(DataTable dataTable)
        {
            if (ModelHelper.HasColumn(dataTable, "id"))
            {
                this.ID = (int)dataTable.Rows[0]["id"];
            }
            if (ModelHelper.HasColumn(dataTable, "title"))
            {
                this.Title = (string)dataTable.Rows[0]["title"];
            }
            if (ModelHelper.HasColumn(dataTable, "blogContent"))
            {
                this.BlogContent = (string)dataTable.Rows[0]["blogContent"];
            }
            if (ModelHelper.HasColumn(dataTable, "authorID"))
            {
                this.AuthorID = dataTable.Rows[0]["authorID"]==null?1:Convert.ToInt32(dataTable.Rows[0]["authorID"]);
            }
            if (ModelHelper.HasColumn(dataTable, "categoryID"))
            {
                this.CategoryID = (int)dataTable.Rows[0]["categoryID"];
            }
            if (ModelHelper.HasColumn(dataTable, "isEssence"))
            {
                this.IsEssence = (bool)dataTable.Rows[0]["isessence"];
            }
            if (ModelHelper.HasColumn(dataTable, "isRecommand"))
            {
                this.IsRecommand = (bool)dataTable.Rows[0]["isRecommand"];
            }
            if (ModelHelper.HasColumn(dataTable, "linkUrl"))
            {
                this.LinkUrl = (string)dataTable.Rows[0]["linkUrls"];
            }
            if (ModelHelper.HasColumn(dataTable, "publishTime"))
            {
                this.PublishTime = (DateTime)dataTable.Rows[0]["publishTimes"];
            }
        }
    }
}
